package com.sunwayworld.common.web.taglib;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * Generated tag class.
 */
public class DisplayPage extends BodyTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String skin ="";

	private int currPage;

	private int num;

	private int numPerPage;

	private int pageNum;

	private String turnMethod = "turnPageInLib";

	public DisplayPage() {
		super();
	}

	public int doStartTag() throws JspException {
		JspWriter out = super.pageContext.getOut();
		

		HttpServletRequest httprequest=(HttpServletRequest)pageContext.getRequest();
		String basepath=httprequest.getContextPath();
		String skin2="/images/htmlobject";
		
		skin=basepath+skin2;
		
		if (currPage > pageNum) {
			currPage = pageNum;
		}
		if (currPage < 1) {
			currPage = 1;
		}

		try {
			out.println("<script language=\"javascript\">");
			out.println("function checkCurrPage(obj){");
			out.println("  var val=obj.value;");
			out.println("  if (val!=\"\"&&!isNaN(val)){");
			out.println("    obj.value==\"" + currPage + "\";//当前页");
			out.println("  }else{");
			out.println("    if (parseInt(val)>\"" + pageNum + "\"){//最大页");
			out.println("      obj.value=" + pageNum);
			out.println("    }");
			out.println("    if (parseInt(val)<1){");
			out.println("      obj.value=1;");
			out.println("    }");
			out.println("  }");
			out.println("}");
			out.println("function turnPageInLib(page){");
			out.println("  with(document.forms(0)){");
			out.println("    currPage.value=page;");
			out.println("    submit();");
			out.println("  }");
			out.println("}");

			out.println("</script>");
			if (currPage > 1) {
				out.print(getMethod(1, "<img src=" + skin
						+ "/button_first.jpg border=0 alt=第1页>")
						+ " "
						+ getMethod(currPage - 1, "<img src=" + skin
								+ "/button_pre.jpg border=0 alt=第"
								+ (currPage - 1) + "页>"));
			} else {
				out.print("<img src=" + skin
						+ "/button_first_disabled.jpg border=0> <img src="
						+ skin + "/button_pre_disabled.jpg border=0>");
			}
			out.print("  ");
			if (currPage < pageNum) {
				out.print(getMethod(currPage + 1, "<img src=" + skin
						+ "/button_next.jpg border=0 alt=第" + (currPage + 1)
						+ "页>")
						+ " "
						+ getMethod(pageNum, "<img src=" + skin
								+ "/button_last.jpg border=0 alt=第" + pageNum
								+ "页>"));
			} else {
				out.print("<img src=" + skin
						+ "/button_next_disabled.jpg border=0> <img src="
						+ skin + "/button_last_disabled.jpg border=0>");
			}
			out.println("  ");

			out
					.print("<input type=text size=3 class=INPUTTEXT name=input_page value="
							+ currPage
							+ " onblur=\"checkCurrPage(this)\"> "
							+ "页/共" + pageNum + "页");
			out.print("记录数:" + num);
			out
					.println(" <input class=INPUTBUTTON type=button name=but_page value=go onclick=\""
							+ turnMethod
							+ "(document.forms(0).input_page.value)" + "\">");
		} catch (IOException ex) {
		}
		return SKIP_BODY;

	}

	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	public int geCurrPage() {
		return currPage;
	}

	public void setCurrPage(int value) {
		currPage = value;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int value) {
		num = value;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setPageNum(int value) {
		this.pageNum = value;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setNumPerPage(int value) {
		numPerPage = value;
	}

	public String getTurnMethod() {
		return turnMethod;
	}

	public void setTurnMethod(String value) {
		this.turnMethod = value;
	}

	private String getMethod(int page, String lstr) {
		return "<a href=\"#\" onclick=\"return " + turnMethod + "(" + page
				+ ");" + "\">" + lstr + "</a>";

	}
}
